'use strict';

const {Service} = require("ee-core");
const Log = require("ee-core/log");
const fs = require("fs")
const path = require("path");
const ps = require("ee-core/ps");

class WallpaperService extends Service {
  constructor(ctx) {
    super(ctx);
  }

  /**
   * 上传文件到smms
   */
  async uploadFileToSMMS(tmpFile) {
    const res = {
      code: 1000,
      message: 'unknown error',
    };

    try {
      const headersObj = {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'aaaaaaaaaaaaa' // 请修改这个token，用你自己的账号token
      };
      const url = 'https://sm.ms/api/v2/upload';
      const response = await this.app.curl(url, {
        method: 'POST',
        headers: headersObj,
        files: {
          smfile: tmpFile,
        },
        dataType: 'json',
        timeout: 15000,
      });
      const result = response.data;
      if (this.app.config.env === 'local') {
        Log.info('[WallpaperService] [uploadFileToSMMS]: info result:%j', result);
      }
      if (result.code !== 'success') {
        Log.error('[WallpaperService] [uploadFileToSMMS]: res error result:%j', result);
      }
      return result;
    } catch (e) {
      Log.error('[WallpaperService] [uploadFileToSMMS]:  ERROR ', e);
    }

    return res;
  }

}

WallpaperService.toString = () => '[class WallpaperService]'
module.exports = WallpaperService
